from typing import Dict
from .exchange import Exchange, ExchangeStatus
from .order_book_sh import OrderBookSH
from .stock_order import StockOrder

class ExchangeSH(Exchange):
    def __init__(self):
        super().__init__()

    # 下单
    def place_order(self, order: StockOrder):
        order_symbol:str = order.get_stock_symbol()
        if order_symbol not in self.__orderbooks:
            self.__orderbooks[order_symbol] = OrderBookSH()
        order_book: OrderBookSH = self.__orderbooks[order_symbol]
        # 竞价
        if self.__status == ExchangeStatus.PRE_OPEN_AUCTION or self.__status == ExchangeStatus.PRE_CLOSE_AUCTION:
            # 先进行集合竞价
            order_book.auction()
            #再添加订单
            self.__orderbooks[order_symbol].add_order_and_match(order)
        # 在集合竞价阶段，直接添加订单到订单簿，不进行撮合
        elif(self.__status == ExchangeStatus.PRE_OPEN_CALL_AUCTIONING or self.__status == ExchangeStatus.PRE_CLOSE_CALL_AUCTIONING):
            order_book.add_to_book(order)
        # 添加订单并尝试撮合
        elif self.__status == ExchangeStatus.CONTINUOUS_AUCTION:
            self.__orderbooks[order_symbol].add_order_and_match(order)